/*=============================================================

PROGRAMMING TECHNIQUES FOR SUPERCOMPUTERS

Assignment Number 1

Task 3

=============================================================*/

#include <stdio.h>
#include "timing.hh"
#include <iostream>
#include <math.h>
#include <fstream>

int main(int argc, char *argv[]) 
{
	double wcTimeStart,wcTimeEnd,cpuTimeStart,cpuTimeEnd,wcTime, cpTime;	
	std::ofstream fileoutd("plot3.dat");
	fileoutd << "# Plot for Task 3" << std::endl;
	fileoutd << "# N\t MFLOP/sec" << std::endl;

	int N=2000;
	double *a = new double[N]; 
	double *b = new double[N]; 
	for (int i = 0; i<N; ++i)
	{
		a[i] = 1.0;
		b[i] = 2.0;
	}
		
	int No;
	
	
	for (No =1; No<N; No= No+5)
	{
	  double s=0.;
		long  repeat =1;
  		double runtime =0.;
		for (; runtime <0.1; repeat*=2)
		{	
			timing(wcTimeStart,cpuTimeStart);
			for (int r=0; r<repeat; ++r)
			{
				//#pragma nounroll
				for (int i = 0; i<No; ++i)
				{
					s= s+ a[i]*b[i];
				}
				if (s<0.0) std::cout << "Dummy Loop Encountered"<< std::endl;
			}
			timing(wcTimeEnd,cpuTimeEnd);	
			runtime=wcTimeEnd-wcTimeStart;
		}

	    repeat/=2;
	    long noFlops = (No)*2*repeat;
	    double mFlops = noFlops/runtime/1000000;
	    if (No%11==0)
	    std::cout << No <<"\t" << mFlops << std::endl;
	    fileoutd << No <<"\t" << mFlops << std::endl;
	}
	std::cout<< "Finished Task 3. \nRun gnuplot with 'plot3.dat' " << std::endl;
	fileoutd.close();
	delete [] a;
	delete [] b;  
	return 0;
}
